package com.oocl.listener;

import javax.jms.JMSException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.log4j.Logger;

import com.oocl.jms.customer.Consumer;
import com.oocl.jms.customer.MerchantConsumerFactory;
import com.oocl.jms.listenrer.MerchantMessageListener;

@WebListener
public class SystemListener implements ServletContextListener {
	Logger logger = Logger.getLogger(SystemListener.class);
	Consumer consumer = null;

    public void contextInitialized(ServletContextEvent sce) {
    	try {
    		logger.info("MerchantMessageListener is started");
			consumer = MerchantConsumerFactory.getInstance().newConsumer("o2o");
			consumer.receive(new MerchantMessageListener());
		} catch (JMSException e) {
			logger.error("MerchantMessageListener starting appear exeception  "+e.getMessage());
		}
    }


    public void contextDestroyed(ServletContextEvent sce) {
    	logger.info("MerchantMessageListener is closed");
        if(null!=consumer){
        	try {
				consumer.close();
			} catch (JMSException e) {
				logger.error("MerchantMessageListener closing appear exeception  "+e.getMessage());
			}
        }
    }
	
}
